﻿
using Silvermoon.Controls.Base;
using Silvermoon.Visuals;
using System.Drawing;
using Silvermoon.Utils;
using Silvermoon.Images;
using Silvermoon.OpenGL;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Decorates a control with a texture.
    /// </summary>
    public class TextureDecorator:Decorator
    {
        private BackgroundVisual background;
        private Color color;
        private bool gradianted;

        public TextureDecorator()
            : base()
        {
            background = new BackgroundVisual { Texture = SysImages.FlatCorners };
            Visuals = new Visual[] { background };
        }

        /// <summary>
        /// Gets or sets the texture to render the background, otherwise null.
        /// </summary>
        public Texture Texture
        {
            get { return background.Texture; }
            set
            {
                if (background.Texture != value)
                {
                    background.Texture = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public Color Color
        {
            get { return color; }
            set
            {
                if (color != value)
                {
                    color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        /// <summary>
        /// Gets or sets whether to use a gradianted background.
        /// </summary>
        public bool Gradianted
        {
            get { return gradianted; }
            set
            {
                if (gradianted != value)
                {
                    gradianted = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }


        protected override void OnLayout()
        {
            background.Size = size;
            if (CheckFlag(InvalidationFlag.Color))
            {
                background.Colors = gradianted ? ColorUtil.CreateGradientColor(color) : ColorUtil.SingleColor(color);
                
            }
            base.OnLayout();
        }
    }
}
